package com.gpframework.module.ctn.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.ctn.domain.GpCtnOrder;
import com.gpframework.module.ctn.domain.GpCtnReachPort;
import com.gpframework.module.ctn.domain.GpTelexRelease;
import com.gpframework.module.ctn.domain.req.GpTelexReleaseAdd;
import com.gpframework.module.ctn.domain.req.GpTelexReleaseUpdate;
import com.gpframework.module.ctn.domain.vo.GpTelexReleaseExcel;
import com.gpframework.module.ctn.domain.vo.GpTelexReleaseVo;
import com.gpframework.module.ctn.mapper.GpTelexReleaseMapper;
import com.gpframework.module.ctn.service.IGpTelexReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpTelexRelease
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 装柜电放填写Service业务层处理
 */
@Service
public class GpTelexReleaseServiceImpl extends ServiceImpl<GpTelexReleaseMapper, GpTelexRelease> implements IGpTelexReleaseService {

    @Autowired
    private GpCtnOrderServiceImpl gpCtnOrderService;
    @Autowired
    private GpCtnArabgeServiceImpl gpCtnArabgeService;
    @Autowired
    private GpPackagesInformationServiceImpl gpPackagesInformationService;
    @Autowired
    private GpCtnReachPortServiceImpl gpCtnReachPortService;
    @Autowired
    private GpTelexReleaseServiceImpl gpTelexReleaseService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [lqw]
     * @Return List<GpTelexReleaseVo>
     */
    @Override
    public List<GpTelexReleaseVo> listVo(QueryWrapper<GpTelexRelease> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [id]
     * @Return GpTelexReleaseVo
     */
    @Override
    public GpTelexReleaseVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public List<GpTelexReleaseExcel> gpTelexReleaseImport(List<GpTelexReleaseExcel> excelList, Long brandId, Long storeId) {
        //导入失败的列表
        List<GpTelexReleaseExcel> failureList = new ArrayList<>();
        //装柜电放填写
        GpTelexRelease gpTelexRelease;

        for (int i = 0; i < excelList.size(); i++) {
            GpTelexReleaseExcel excel = excelList.get(i);
            if (StringUtils.isNull(excel)) {
                excel = new GpTelexReleaseExcel();
                excel.setSubscript(i+1);
                excel.setMsg("读取数据失败");
                failureList.add(excel);
                continue;
            }
            gpTelexRelease = Convert.convert(new TypeReference<GpTelexRelease>() {}, excel);
            this.save(gpTelexRelease);
        }
        return failureList;
    }

    @Override
    public boolean createTeleRelease(GpTelexReleaseAdd gpTelexReleaseAdd) {
        GpTelexRelease gpTelexRelease = Convert.convert(new TypeReference<GpTelexRelease>() {}, gpTelexReleaseAdd);
        GpCtnOrder gpCtnOrder = gpCtnOrderService.getById(gpTelexReleaseAdd.getCtnOrderId());
        if (gpCtnOrder!=null){
            gpTelexRelease.setAgentCtnNo(gpCtnOrder.getAgentCtnNo());
            gpTelexRelease.setAgentCompany(gpCtnOrder.getAgentCompany());
            gpTelexRelease.setBillNo(gpCtnOrder.getBillNo());
            gpTelexRelease.setCtnNo(gpCtnOrder.getCtnNo());
            gpTelexRelease.setSealNo(gpCtnOrder.getSealNo());
            gpTelexRelease.setDestination(gpCtnOrder.getDestination());
        }

        return this.save(gpTelexRelease);
    }

    @Override
    public boolean editCtnManagement(GpTelexReleaseUpdate gpTelexReleaseUpdate) {
        GpTelexRelease telexReleaseUpdate = this.getOne(new QueryWrapper<GpTelexRelease>().eq("ctn_order_id", gpTelexReleaseUpdate.getId()));

        //修改订货柜信息
        if (telexReleaseUpdate.getCtnOrderId()!=null){
            GpCtnOrder ctnOrder = Convert.convert(new TypeReference<GpCtnOrder>() { }, gpTelexReleaseUpdate);
            ctnOrder.setId(telexReleaseUpdate.getCtnOrderId());
            gpCtnOrderService.updateById(ctnOrder);
        }

        //修改到港信息
        GpCtnReachPort gpCtnReachPort = gpCtnReachPortService.getOne(new QueryWrapper<GpCtnReachPort>().eq("ctn_order_id", gpTelexReleaseUpdate.getId()));
        if (gpCtnReachPort!=null){
            gpCtnReachPort.setFreightPrice(gpTelexReleaseUpdate.getFreightPrice());
            gpCtnReachPort.setReachDestinationDate(gpTelexReleaseUpdate.getReachDestinationDate());
            GpCtnReachPort gpCtnReachPortUpdate = Convert.convert(new TypeReference<GpCtnReachPort>() {}, gpCtnReachPort);
            gpCtnReachPortService.updateById(gpCtnReachPortUpdate);
        }

        //修改电放信息
        if (telexReleaseUpdate!=null){
            GpTelexRelease gpTelexRelease = Convert.convert(new TypeReference<GpTelexRelease>() {}, telexReleaseUpdate);
            gpTelexRelease.setTelexReleaseDate(gpTelexReleaseUpdate.getTelexReleaseDate());
            this.updateById(gpTelexRelease);
        }

        return true;
    }


}
